import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../../dd_core/dd_config.dart';
import '../../../dd_core/dd_hive_dao.dart';
import '../../../dd_core/dd_http.dart';
import '../../../dd_core/dd_log_service.dart';
import '../../../dd_models/dd_hive_works_model.dart';
import 'dd_home_second_controller.dart';

class DDUserSpaceController extends GetxController {
  final HiveWorksModel userModel;
  DDUserSpaceController(this.userModel);

  var userModelObx = HiveWorksModel().obs;

  final userList = <HiveWorksModel>[].obs;

  @override
  void onClose() {
    userModelObx.close();
    userList.close();
    super.onClose();
  }

  @override
  onInit() {
    super.onInit();
    userModelObx.value = userModel;
    getUserList();
  }

  getUserList() async {
    final prefs = await SharedPreferences.getInstance();
    String cureenUid = await prefs.getString(DDKey.cureenUid) ?? "";
    List<HiveWorksModel>? d = await DDHiveDao.getUserFencingList(
        uid: cureenUid, userId: userModelObx.value.userId ?? '');
    if (d != null) {
      userList.value = d;
      userList.refresh();
    }
  }

  Future<void> updateFollow() async {
    if (userModelObx.value.myFollow != null) {
      userModelObx.value.myFollow = !userModelObx.value.myFollow!;
    } else {
      userModelObx.value.myFollow = true;
    }

    if (userModelObx.value.myFollow ?? false) {
      EasyLoading.show();
      Map<String, dynamic>? result =
          await addFriend(userModelObx.value.userId ?? '');
      EasyLoading.dismiss();
      if (result != null && result['code'] == 0) {
        await DDHiveDao.updateFencingFollow(
            hiveWorksModel: userModelObx.value,
            myFollow: userModelObx.value.myFollow!);
        await Get.find<DDHomeSecondController>().getList();
        userModelObx.refresh();
        if (userModelObx.value.myFollow!) {
          EasyLoading.showToast("Follow Successfully",
              duration: Duration(seconds: 2));
        }
      }
    } else {
      EasyLoading.show();
      Map<String, dynamic>? result =
          await unfriend(userModelObx.value.userId ?? '');
      EasyLoading.dismiss();
      if (result != null && result['code'] == 0) {
        await DDHiveDao.updateFencingFollow(
            hiveWorksModel: userModelObx.value,
            myFollow: userModelObx.value.myFollow!);
        await Get.find<DDHomeSecondController>().getList();
        userModelObx.refresh();
        if (!userModelObx.value.myFollow!) {
          EasyLoading.showToast("Unfollow Successfully",
              duration: Duration(seconds: 2));
        }
      }
    }
  }


  updateMyLike(HiveWorksModel m) async {
    await DDHiveDao.updateFencingLikes(hiveWorksModel: m);
    await getUserList();
    await Get.find<DDHomeSecondController>().getList();
  }

  Future<void> updateBlock() async {
    if (userModelObx.value.myBlock != null) {
      userModelObx.value.myBlock = !userModelObx.value.myBlock!;
    } else {
      userModelObx.value.myBlock = true;
    }
    DDLogService().ddGa(page: "block");

    if (userModelObx.value.myBlock ?? false) {
      EasyLoading.show();
      var p = {
        "broadcasterId": userModelObx.value.userId ?? '',
        "complainCategory": 'Block',
      };

      Map<String, dynamic>? result = await insertRecord(p);
      EasyLoading.dismiss();
      if (result != null && result['code'] == 0) {
        await DDHiveDao.updateFencingBlock(
            hiveWorksModel: userModelObx.value,
            myBlock : userModelObx.value.myBlock!);
        await Get.find<DDHomeSecondController>().getList();
        userModelObx.refresh();
        if (userModelObx.value.myBlock!) {
          EasyLoading.showToast("Block successful",
              duration: Duration(seconds: 2));
          navigator?.pop();
        }
      }
    } else {
      EasyLoading.show();
      Map<String, dynamic>? result =
          await removeBlock(userModelObx.value.userId ?? '');
      EasyLoading.dismiss();
      if (result != null && result['code'] == 0) {
        await DDHiveDao.updateFencingBlock(
            hiveWorksModel: userModelObx.value,
            myBlock: userModelObx.value.myBlock!);
        await Get.find<DDHomeSecondController>().getList();
        userModelObx.refresh();
        if (userModelObx.value.myBlock!) {
          EasyLoading.showToast("Block successful",
              duration: Duration(seconds: 2));
        }
      }
    }
  }

  report(String content) async {
    DDLogService().ddGa(page: "report");

    EasyLoading.show();
    var p = {
      "broadcasterId": userModelObx.value.userId ?? '',
      "complainCategory": 'Report',
      "complainSub": content,
    };
    Map<String, dynamic>? result = await insertRecord(p);
    EasyLoading.dismiss();
    if (result != null && result['code'] == 0) {
      EasyLoading.showToast("Report Successfully",
          duration: Duration(seconds: 2));
    }
  }
}
